//
//  HNSH_SafeHomeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_SafeHomeViewController.h"

//view
#import "HNSH_SafeItemView.h"

//model
#import "ST_TimeDownToors.h"

//vc
#import "HNSH_ChangePwdViewController.h"
#import "HNSH_ChangePayPwdViewController.h"

@interface HNSH_SafeHomeViewController ()

/* 手机号 */
@property (nonatomic, strong) HNSH_SafeItemView *phoneView;
/* 手机号 */
@property (nonatomic, copy) NSString *phoneString;
/* 验证码 */
@property (nonatomic, strong) HNSH_SafeItemView *codeView;
/* 验证码 */
@property (nonatomic, copy) NSString *codeString;

@end

@implementation HNSH_SafeHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.phoneString = @"";
    self.codeString = @"";
    
    [self showNavTitle:@"安全检测" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    [self configUI];
}

- (void)configUI {
    //手机号
    self.phoneView = [[HNSH_SafeItemView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 50) type:0];
    self.phoneView.titleString = @"手机号：";
    self.phoneView.placeholder = @"请输入手机号";
    [self.view addSubview:self.phoneView];
    
    if ([ST_UserManager alreadyLogin]) {
        self.phoneView.contentString = [ST_UserManager shareInstance].user.phone;
        self.phoneString = [ST_UserManager shareInstance].user.phone;
        self.phoneView.userInteractionEnabled = NO;
    }
    
    //验证码
    self.codeView = [[HNSH_SafeItemView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+50, kScreenWidth, 50) type:1];
    self.codeView.titleString = @"验证码：";
    self.codeView.placeholder = @"请输入验证码";
    [self.view addSubview:self.codeView];
    
    //下一步
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame = CGRectMake(30, self.codeView.origin.y+self.codeView.height+140, kScreenWidth-60, 50);
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    nextButton.backgroundColor = [UIColor appGreenColor];
    nextButton.layer.cornerRadius = 25.0f;
    nextButton.layer.masksToBounds = YES;
    [self.view addSubview:nextButton];
    
    WeakSelf(weakSelf);
    self.phoneView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.phoneString = inputString;
    };
    self.codeView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.codeString = inputString;
    };
    self.codeView.buttonBlock = ^(UIButton * _Nonnull sendButton) {
        [weakSelf loadSendCodeWithButton:sendButton];
    };
    [[nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadVerifyCode];
    }];
}

#pragma mark networking
//发送验证码
- (void)loadSendCodeWithButton:(UIButton *)sendButton {
    if (![self.phoneString isNotBlank]) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    NSInteger type = KSendCodeType_ResetPassword_UnLogin;
    if (self.style == HNSH_SafeHomeStyle_ForgetPassword) {
        type = KSendCodeType_ResetPassword_UnLogin;
    } else if (self.style == HNSH_SafeHomeStyle_ChangePassword) {
        type = KSendCodeType_ResetPassword_Login;
    } else if (self.style == HNSH_SafeHomeStyle_ChangePay) {
        type = KSendCodeType_ChangePaymentPassword;
    }
    [HNSH_UserNetServer requestSendCodeWithUserName:self.phoneString type:type success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [[ST_TimeDownToors shareTimer] startTimerWithTime:60 complate:^(NSInteger time) {
                NSString *timeStr = [NSString stringWithFormat:@"%lds",time];
                [sendButton setTitle:timeStr forState:UIControlStateNormal];
                sendButton.enabled = NO;
                if (time == 0) {
                    [sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
                    sendButton.enabled = YES;
                }
            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadVerifyCode {
    if (![self.phoneString isNotBlank]) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    if (![self.codeString isNotBlank]) {
        [self.view makeToast:@"请输入验证码"];
        return;
    }
    NSInteger type = KSendCodeType_ResetPassword_UnLogin;
    if (self.style == HNSH_SafeHomeStyle_ForgetPassword) {
        type = KSendCodeType_ResetPassword_UnLogin;
    } else if (self.style == HNSH_SafeHomeStyle_ChangePassword) {
        type = KSendCodeType_ResetPassword_Login;
    } else if (self.style == HNSH_SafeHomeStyle_ChangePay) {
        type = KSendCodeType_ChangePaymentPassword;
    }
    [HNSH_UserNetServer requestVerifyCodeWithUserName:self.phoneString type:type code:self.codeString success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (self.style == HNSH_SafeHomeStyle_ChangePay) {//支付密码
                HNSH_ChangePayPwdViewController *vc = [[HNSH_ChangePayPwdViewController alloc]init];
                vc.smsCode = self.codeString;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                HNSH_ChangePwdViewController *vc = [[HNSH_ChangePwdViewController alloc]init];
                vc.mobile = self.phoneString;
                vc.smsCode = self.codeString;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
